#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tcpmanager.h"

#include <QFile>
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_loginDlg = new LoginDialog(this);
    m_loginDlg->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setCentralWidget(m_loginDlg);

    // 创建登录窗口与注册窗口的信号连接
    connect(m_loginDlg, &LoginDialog::switchRegister, this, &MainWindow::SlotSwitchReg);
    // 连接登录界面的忘记密码与重置界面
    connect(m_loginDlg, &LoginDialog::switchReset, this, &MainWindow::SlotSwitchReset);
    // 连接聊天界面信号
    connect(TcpManager::getInstance().get(), &TcpManager::sig_swich_chatdlg, this, &MainWindow::SlotSwitchChat);

    // emit TcpManager::getInstance()->sig_swich_chatdlg();

}

MainWindow::~MainWindow()
{
    delete ui;

    /*
    if (m_loginDlg) {
        delete m_loginDlg;
        m_loginDlg = nullptr;
    }

    if (m_registerDlg) {
        delete m_registerDlg;
        m_loginDlg = nullptr;
    }
    */
}


void MainWindow::SlotSwitchReg()
{
    m_registerDlg = new RegisterDialog(this);
    m_registerDlg->hide();
    m_registerDlg->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);

    setCentralWidget(m_registerDlg);
    m_loginDlg->hide();
    m_registerDlg->show();

    // 连接注册界面返回登录信号
    connect(m_registerDlg, &RegisterDialog::sigSwitchLogin, this, &MainWindow::SlotSwitchLogin);
}

void MainWindow::SlotSwitchLogin()
{
    // 创建一个CentralWidget, 并将其设置为MainWindow的中心部件
    m_loginDlg = new LoginDialog(this);
    m_loginDlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    setCentralWidget(m_loginDlg);

    m_registerDlg->hide();
    m_loginDlg->show();
    // 连接登录界面注册信号
    connect(m_loginDlg, &LoginDialog::switchRegister, this, &MainWindow::SlotSwitchReg);
    //连接登录界面忘记密码信号
    connect(m_loginDlg, &LoginDialog::switchReset, this, &MainWindow::SlotSwitchReset);
}

void MainWindow::SlotSwitchReset()
{
    // 创建一个CentralWidget（重置密码的界面）, 并将其设置为MainWindow的中心部件
    m_resetDlg = new ResetDialog(this);
    m_resetDlg->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setCentralWidget(m_resetDlg);

    m_loginDlg->hide();
    m_resetDlg->show();
    // 重置密码完毕后返回登录界面
    connect(m_resetDlg, &ResetDialog::switchLogin, this, &MainWindow::SlotSwitchLoginFromResetDlg);
}

void MainWindow::SlotSwitchLoginFromResetDlg()
{
    // 创建一个CentralWidget, 并将其设置为MainWindow的中心部件
    m_loginDlg = new LoginDialog(this);
    m_loginDlg->setWindowFlags(Qt::CustomizeWindowHint|Qt::FramelessWindowHint);
    setCentralWidget(m_loginDlg);

    m_resetDlg->hide();
    m_loginDlg->show();
    // 连接登录界面忘记密码信号
    connect(m_loginDlg, &LoginDialog::switchReset, this, &MainWindow::SlotSwitchReset);
    // 连接登录界面注册信号
    connect(m_loginDlg, &LoginDialog::switchRegister, this, &MainWindow::SlotSwitchReg);
}

// 登录校验成功后，跳转到聊天界面
void MainWindow::SlotSwitchChat()
{
    m_chatDlg = new ChatDialog(this);
    m_chatDlg->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    setCentralWidget(m_chatDlg);

    m_loginDlg->hide();
    m_chatDlg->show();

    this->setMinimumSize(QSize(1050, 900));
    this->setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
}
